<template>
    <div class="sidebar">

        <el-menu class="sidebar-el-menu" :default-active="onRoutes" :collapse="collapse" background-color="#324157"
            text-color="#bfcbd9" active-text-color="#20a0ff" unique-opened router>
            <!-- v-for -->
            <template v-for="item in items">
                <!-- v-if -->
                <template v-if="item.subs">
                    <el-submenu :index="item.index" :key="item.index">

                        <template #title>
                            <i :class="item.icon"></i>
                            <span>{{ item.title }}</span>
                        </template>

                        <template v-for="subItem in item.subs">
                            <el-submenu v-if="subItem.subs" :index="subItem.index" :key="subItem.index">
                                <template #title>{{ subItem.title }}</template>
                                <el-menu-item v-for="(threeItem, i) in subItem.subs" :key="i" :index="threeItem.index">
                                    {{ threeItem.title }}</el-menu-item>
                            </el-submenu>
                            <el-menu-item v-else :index="subItem.index" :key="subItem.index">{{ subItem.title }}
                            </el-menu-item>
                        </template>
                        
                    </el-submenu>
                </template>
                <!-- v-else -->
                <template v-else>
                    <el-menu-item :index="item.index" :key="item.index">
                        <i :class="item.icon"></i>
                        <template #title>{{ item.title }}</template>
                    </el-menu-item>
                </template>

            </template>

        </el-menu>

    </div>
</template>

<script>
import { computed } from "vue";
import { useStore } from "vuex";
import { useRoute } from "vue-router";
export default {
    setup() {
        let items = [
            {
                icon: "el-icon-lx-home",
                index: "/dashboard",
                title: "系统首页",
            },
            {
                icon: "el-icon-lx-people",
                index: '/doctor',
                title: "人员管理",
                subs: [
                    {
                        icon: "el-icon-lx-emoji",
                        index: "/doctor",
                        title: "人员信息",
                    },
                    {
                        icon: "el-icon-lx-emoji",
                        index: "/doctorForm",
                        title: "新增人员",
                    },
                ]
            },
            {
                icon: "el-icon-lx-link",
                index: '/medicine',
                title: "药品管理",
                subs: [
                    {
                        icon: "el-icon-lx-emoji",
                        index: "/medicine",
                        title: "药品信息",
                    },
                    {
                        icon: "el-icon-lx-emoji",
                        index: "/medicineForm",
                        title: "新增药品",
                    },
                ]
            },
            {
                icon: "el-icon-lx-cart",
                index: '/purchase',
                title: "药品采购",
                subs: [
                    {
                        icon: "el-icon-lx-emoji",
                        index: "/supplier",
                        title: "供应商信息",
                    },
                    {
                        icon: "el-icon-lx-emoji",
                        index: "/supplierForm",
                        title: "新增供应商",
                    },
                    {
                        icon: "el-icon-lx-emoji",
                        index: "/bill",
                        title: "采购信息",
                    },
                    {
                        icon: "el-icon-lx-emoji",
                        index: "/billForm",
                        title: "采购登记",
                    }
                ]
            },
            {
                icon: "el-icon-lx-shop",
                index: '/sell',
                title: "药品销售",
                subs: [
                    {
                        icon: "el-icon-lx-emoji",
                        index: "/patientBill",
                        title: "销售信息",
                    },
                    {
                        icon: "el-icon-lx-emoji",
                        index: "/patientBillForm",
                        title: "销售登记",
                    }
                ]
            },
            {
                icon: "el-icon-lx-group",
                index: '/statisticModule',
                title: "数据统计",
                subs: [
                    {
                        icon: "el-icon-lx-emoji",
                        index: "/statisticLine",
                        title: "条形图统计",
                    },
                    {
                        icon: "el-icon-lx-emoji",
                        index: "/statisticBar",
                        title: "柱状图统计",
                    },
                    {
                        icon: "el-icon-lx-emoji",
                        index: "/statisticPie",
                        title: "饼状图统计",
                    }
                ]
            },

            {
                icon: "el-icon-lx-group",
                index: '/account',
                title: "账号管理",
                subs: [
                    {
                        icon: "el-icon-lx-group",
                        index: "/admin",
                        title: "账号信息",
                    },
                    {
                        icon: "el-icon-lx-group",
                        index: "/adminForm",
                        title: "新增账号",
                    },
                    {
                        icon: "el-icon-lx-emoji",
                        index: "/adminUpdateForm",
                        title: "修改密码",
                    }
                ]
            }
        ];

        const initItems = () => {
            let role = localStorage.getItem("role");
            console.log("role = " + role);
            if (role === 1 || role === '1') {
                items = [
                    {
                        icon: "el-icon-lx-home",
                        index: "/dashboard",
                        title: "系统首页",
                    },
                    {
                        icon: "el-icon-lx-link",
                        index: '/medicine',
                        title: "药品管理",
                        subs: [
                            {
                                icon: "el-icon-lx-emoji",
                                index: "/medicine",
                                title: "药品信息",
                            }
                        ]
                    },
                    {
                        icon: "el-icon-lx-shop",
                        index: '/sell',
                        title: "药品销售",
                        subs: [
                            {
                                icon: "el-icon-lx-emoji",
                                index: "/patientBill",
                                title: "销售信息",
                            },
                            {
                                icon: "el-icon-lx-emoji",
                                index: "/patientBillForm",
                                title: "销售登记",
                            }
                        ]
                    },
                    {
                        icon: "el-icon-lx-group",
                        index: '/statisticModule',
                        title: "数据统计",
                        subs: [
                            {
                                icon: "el-icon-lx-emoji",
                                index: "/statisticLine",
                                title: "条形图统计",
                            },
                            {
                                icon: "el-icon-lx-emoji",
                                index: "/statisticBar",
                                title: "柱状图统计",
                            }
                        ]
                    },
                    
                    {
                        icon: "el-icon-lx-group",
                        index: '/account',
                        title: "账号管理",
                        subs: [
                            {
                                icon: "el-icon-lx-emoji",
                                index: "/adminUpdateForm",
                                title: "修改密码",
                            }
                        ]
                    }
                ];
            } else if (role === 2 || role === '2') {
                items = [
                    {
                        icon: "el-icon-lx-home",
                        index: "/dashboard",
                        title: "系统首页",
                    },
                    {
                        icon: "el-icon-lx-link",
                        index: '/medicine',
                        title: "药品管理",
                        subs: [
                            {
                                icon: "el-icon-lx-emoji",
                                index: "/medicine",
                                title: "药品信息",
                            },
                            // {
                            //     icon: "el-icon-lx-emoji",
                            //     index: "/medicineForm",
                            //     title: "新增药品",
                            // },
                        ]
                    },
                    {
                        icon: "el-icon-lx-cart",
                        index: '/purchase',
                        title: "药品采购",
                        subs: [
                            {
                                icon: "el-icon-lx-emoji",
                                index: "/supplier",
                                title: "供应商信息",
                            },
                            // {
                            //     icon: "el-icon-lx-emoji",
                            //     index: "/supplierForm",
                            //     title: "新增供应商",
                            // },
                            {
                                icon: "el-icon-lx-emoji",
                                index: "/bill",
                                title: "采购信息",
                            },
                            {
                                icon: "el-icon-lx-emoji",
                                index: "/billForm",
                                title: "采购登记",
                            }
                        ]
                    },
                    {
                        icon: "el-icon-lx-group",
                        index: '/statisticModule',
                        title: "数据统计",
                        subs: [
                            {
                                icon: "el-icon-lx-emoji",
                                index: "/statisticLine",
                                title: "条形图统计",
                            },
                            {
                                icon: "el-icon-lx-emoji",
                                index: "/statisticBar",
                                title: "柱状图统计",
                            }
                        ]
                    },
                    {
                        icon: "el-icon-lx-group",
                        index: '/account',
                        title: "账号管理",
                        subs: [
                            {
                                icon: "el-icon-lx-emoji",
                                index: "/adminUpdateForm",
                                title: "修改密码",
                            }
                        ]
                    }
                ];
            }else if(role === 3 || role === '3'){
                 items = [
                    {
                        icon: "el-icon-lx-home",
                        index: "/dashboard",
                        title: "系统首页",
                    },
                    {
                        icon: "el-icon-lx-link",
                        index: '/medicine',
                        title: "药品管理",
                        subs: [
                            {
                                icon: "el-icon-lx-emoji",
                                index: "/medicine",
                                title: "药品信息",
                            },
                            // {
                            //     icon: "el-icon-lx-emoji",
                            //     index: "/medicineForm",
                            //     title: "新增药品",
                            // },
                        ]
                    },
                    {
                        icon: "el-icon-lx-shop",
                        index: '/sell',
                        title: "药品销售",
                        subs: [
                            {
                                icon: "el-icon-lx-emoji",
                                index: "/patientBill",
                                title: "销售信息",
                            },
                            // {
                            //     icon: "el-icon-lx-emoji",
                            //     index: "/patientBillForm",
                            //     title: "销售登记",
                            // }
                        ]
                    },
                    {
                        icon: "el-icon-lx-group",
                        index: '/statisticModule',
                        title: "数据统计",
                        subs: [
                            {
                                icon: "el-icon-lx-emoji",
                                index: "/statisticLine",
                                title: "条形图统计",
                            },
                            {
                                icon: "el-icon-lx-emoji",
                                index: "/statisticBar",
                                title: "柱状图统计",
                            }
                        ]
                    },                   
                    {
                        icon: "el-icon-lx-group",
                        index: '/account',
                        title: "账号管理",
                        subs: [
                            {
                                icon: "el-icon-lx-emoji",
                                index: "/adminUpdateForm",
                                title: "修改密码",
                            }
                        ]
                    }
                ];
            }else if (role === 4 || role === '4') {
                items = [
                    {
                        icon: "el-icon-lx-home",
                        index: "/dashboard",
                        title: "系统首页",
                    },
                    {
                        icon: "el-icon-lx-link",
                        index: '/medicine',
                        title: "药品管理",
                        subs: [
                            {
                                icon: "el-icon-lx-emoji",
                                index: "/medicine",
                                title: "药品信息",
                            },
                            {
                                icon: "el-icon-lx-emoji",
                                index: "/medicineForm",
                                title: "新增药品",
                            },
                        ]
                    },
                    {
                        icon: "el-icon-lx-cart",
                        index: '/purchase',
                        title: "药品采购",
                        subs: [
                            {
                                icon: "el-icon-lx-emoji",
                                index: "/supplier",
                                title: "供应商信息",
                            },
                            {
                                icon: "el-icon-lx-emoji",
                                index: "/supplierForm",
                                title: "新增供应商",
                            },
                            {
                                icon: "el-icon-lx-emoji",
                                index: "/bill",
                                title: "采购信息",
                            },
                            // {
                            //     icon: "el-icon-lx-emoji",
                            //     index: "/billForm",
                            //     title: "采购登记",
                            // }
                        ]
                    },
                    {
                        icon: "el-icon-lx-group",
                        index: '/statisticModule',
                        title: "数据统计",
                        subs: [
                            {
                                icon: "el-icon-lx-emoji",
                                index: "/statisticLine",
                                title: "条形图统计",
                            },
                            {
                                icon: "el-icon-lx-emoji",
                                index: "/statisticBar",
                                title: "柱状图统计",
                            }
                        ]
                    },
                    {
                        icon: "el-icon-lx-group",
                        index: '/account',
                        title: "账号管理",
                        subs: [
                            {
                                icon: "el-icon-lx-emoji",
                                index: "/adminUpdateForm",
                                title: "修改密码",
                            }
                        ]
                    }
                ];
            }
        };
        initItems();

        const route = useRoute();

        const onRoutes = computed(() => {
            return route.path;
        });

        const store = useStore();
        const collapse = computed(() => store.state.collapse);

        return {
            items,
            onRoutes,
            collapse,
        };
    },
};
</script>

<style scoped>
.sidebar {
    display: block;
    position: absolute;
    left: 0;
    top: 70px;
    bottom: 0;
    overflow-y: scroll;
}
.sidebar::-webkit-scrollbar {
    width: 0;
}
.sidebar-el-menu:not(.el-menu--collapse) {
    width: 250px;
}
.sidebar > ul {
    height: 100%;
}
</style>
